home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / DCLAP 6d / dclap6d / DClap / Dvibrant.c < prev    next >
Text File  |  1996-07-05  |  20KB  |  741 lines

  1. /* Dvibrant.c */
  2. /* dgg additions to ncbi/vibrant */
  3.  
  4.  
  5. #include "Dvibrant.h"
  6.  
  7. #ifdef COMP_CWI
  8. #define _asm  asm
  9. #endif
  10.  
  11. #include <ncbi.h>
  12. #include <ncbidraw.h>
  13. #include <ncbiwin.h>
  14. #include <ncbiport.h>
  15. #include <vibincld.h>
  16.  
  17. void Dgg_PenNormal()  /* nlmdraw addition */
  18. {
  19.     Nlm_Black();
  20.     Nlm_Solid();
  21.     Nlm_WidePen(1); /* << !? need Nlm_PenNormal() */
  22. }
  23.  
  24. extern void Dgg_DrawTextbox (Nlm_RectPtr r, Nlm_CharPtr text,
  25.                                ulong len, Nlm_Char jst, Nlm_Boolean gray)
  26. {
  27. /* from Nlm_DrawText which mostly does this, but XWin XDrawString primitive doesn't know about
  28.    multiline text */
  29.    
  30. #ifdef WIN_MAC
  31.   Nlm_Int2      just;
  32.   PenState      pnState;
  33.   Nlm_RectTool  rtool;
  34.  
  35.   if (r != NULL) {
  36.     Nlm_EraseRect (r);
  37.     if (text != NULL && len > 0) {
  38.       Nlm_RecTToRectTool (r, &rtool);
  39.       switch (jst) {
  40.         case 'r':
  41.           just = -1;
  42.           break;
  43.         case 'l':
  44.           just = 0;
  45.           break;
  46.         case 'c':
  47.           just = 1;
  48.           break;
  49.         default:
  50.           just = 0;
  51.           break;
  52.       }
  53.       TextBox (text, len, &rtool, just);
  54.       if (gray) {
  55.         GetPenState (&pnState);
  56.         PenMode (patBic);
  57.         /*PenPat ((ConstPatternParam) grayPat);*/
  58.                 Nlm_Medium();
  59.         PaintRect (&rtool);
  60.         SetPenState (&pnState);
  61.       }
  62.     }
  63.   }
  64. #endif
  65. #ifdef WIN_MSWIN
  66.   Nlm_Int2      format;
  67.   Nlm_Int4      oldcolor;
  68.   Nlm_RectTool  rtool;
  69.  
  70.   if (r != NULL && Nlm_currentHDC != NULL) {
  71.     Nlm_RecTToRectTool (r, &rtool);
  72.     if (Nlm_currentHWnd != NULL) {
  73.       Nlm_EraseRect (r);
  74.     } else {
  75.       FillRect (Nlm_currentHDC, &rtool, GetStockObject (WHITE_BRUSH));
  76.     }
  77.     if (text != NULL && len > 0) {
  78.       switch (jst) {
  79.         case 'r':
  80.           format = DT_RIGHT;
  81.           break;
  82.         case 'l':
  83.           format = DT_LEFT;
  84.           break;
  85.         case 'c':
  86.           format = DT_CENTER;
  87.           break;
  88.         default:
  89.           format = DT_LEFT;
  90.           break;
  91.       }
  92.       if (gray) {
  93.         oldcolor = SetTextColor (Nlm_currentHDC, GetSysColor (COLOR_GRAYTEXT));
  94.       }
  95.       DrawText (Nlm_currentHDC, text, (Nlm_Int2) len, &rtool,
  96.                 format | DT_VCENTER | DT_NOPREFIX);
  97.       if (gray) {
  98.         SetTextColor (Nlm_currentHDC, oldcolor);
  99.       }
  100.     }
  101.   }
  102. #endif
  103.  
  104. #ifdef WIN_MOTIF
  105.   Nlm_Int2   delta;
  106.   Nlm_Int2   height;
  107.   Nlm_Int2   limit;
  108.   Pixmap     pix;
  109.   Nlm_PoinT  pt;
  110.   Nlm_Int2   width;
  111.  
  112.   if (r != NULL && Nlm_currentXDisplay != NULL &&
  113.       Nlm_currentXWindow != 0 && Nlm_currentXGC != NULL) {
  114.     Nlm_EraseRect (r);
  115.     if (text != NULL && len > 0) {
  116.             Nlm_Int4  widlimit, pixwid, linewid, nlines;
  117.             Nlm_Int2    atx, liney;
  118.             char    *cp, *cp0;
  119.             
  120.       pt.x = r->left - Nlm_XOffset;
  121.       pt.y = r->top - Nlm_YOffset;
  122.             nlines= 0;
  123.             cp0= text;
  124.             do {
  125.                 nlines++;
  126.                 cp= Nlm_StrChr(cp0, '\n');  
  127.                 if (cp) cp0= 1 + cp;
  128.             } while (cp);
  129.  
  130.          width= 0;
  131.         widlimit = ABS(r->right - r->left);
  132.             limit = ABS(r->bottom - r->top);
  133.       height = Nlm_LineHeight();
  134.             delta = limit - nlines * height; 
  135.             if (delta > 0) pt.y += delta / 2;   
  136.             liney= pt.y + Nlm_Ascent();
  137.             
  138.             cp0= text;
  139.         if (gray) Nlm_Medium();
  140.             do {
  141.                 nlines++;
  142.                 cp= Nlm_StrChr(cp0, '\n');  
  143.                 if (cp) linewid= cp - cp0;
  144.                 else linewid= len - (cp0 - text);
  145.                     
  146.                 pixwid= Nlm_TextWidth (cp0, linewid);
  147.                 if (pixwid > widlimit) {
  148.                     linewid= Min(linewid, 1 + widlimit / Nlm_MaxCharWidth());
  149.                     pixwid= Nlm_TextWidth(cp0, linewid);
  150.                     while (linewid > 0 && pixwid > widlimit) {
  151.                 linewid--;
  152.                 pixwid = Nlm_TextWidth (cp0, linewid);
  153.                   }
  154.                     }
  155.                 
  156.                 delta = widlimit - pixwid;
  157.                 if (delta>0) switch (jst) {
  158.                     case 'r':
  159.                         atx = pt.x + delta;
  160.                         break;
  161.                     default:
  162.                     case 'l':
  163.                         atx = pt.x;
  164.                         break;
  165.                     case 'c':
  166.                         atx = pt.x + delta / 2;
  167.                         break;
  168.                     }
  169.                  
  170.                 XDrawString (Nlm_currentXDisplay, Nlm_currentXWindow, Nlm_currentXGC,
  171.                                          atx, liney, cp0, (int) linewid);
  172.                 liney += height; 
  173.                 
  174.                 if (cp) {
  175.                     if (linewid < cp - cp0) cp0 += linewid; 
  176.                     else cp0 = 1+cp;
  177.                     }
  178.                 else if (cp0 + linewid < text + len) {
  179.                     cp0= cp0 + linewid;
  180.                     cp= cp0;
  181.                     }
  182.             } while (cp);
  183.     if (gray) Nlm_Solid();
  184.     
  185.     }
  186.   }
  187. #endif
  188. }
  189.  
  190.  
  191.  
  192.  
  193. extern void Dgg_SetParentWindow (Nlm_GraphiC a, Nlm_WindoW newwindow)
  194. {
  195.   Nlm_GraphiC  child, g, p;
  196.  
  197.   g = a;
  198.   if (a != NULL) {
  199.           child= g;
  200.     p = Nlm_GetParent (g);
  201.     while (p != NULL) {
  202.                child= g;
  203.       g = p;
  204.       p = Nlm_GetParent (g);
  205.     }
  206.   /* g == window, child == a primary subview in window */
  207.   if (child != NULL) Nlm_SetParent( child, (Nlm_GraphiC) newwindow);
  208.   }
  209. }
  210.  
  211.  
  212.  
  213. #ifdef WIN_MAC
  214.  
  215. /* LaunchFileThruFinder perpares and sends the Finder OpenSelection event */
  216.  
  217. #include <Processes.h>
  218. #include <GestaltEqu.h>
  219. #include <AppleEvents.h>
  220. #include <Aliases.h>
  221.  
  222.  
  223.         // from MacAppUtilities.cp
  224. OSErr FindProcessBySignature(OSType sig, ProcessSerialNumber* psn, FSSpec* fileSpec)
  225. {
  226.     OSErr err;
  227.     ProcessInfoRec info;
  228.  
  229.     psn->highLongOfPSN = 0;
  230.     psn->lowLongOfPSN  = kNoProcess;
  231.     do {
  232.         err= GetNextProcess(psn);
  233.         if( err == noErr ) {
  234.             info.processInfoLength = sizeof(ProcessInfoRec);
  235.             info.processName = NULL;
  236.             info.processAppSpec = fileSpec;
  237.             err= GetProcessInformation(psn, &info);
  238.         }
  239.     } while( (err == noErr) && info.processSignature != sig );
  240.  
  241.     if( err == noErr ) (*psn) = info.processNumber;
  242.  
  243.     return err;
  244. }  
  245.  
  246.  
  247. OSErr LaunchFileSpecThruFinder( FSSpec fileToLaunch)
  248. {                    
  249.     const        OSType aeSelectionKeyword    = 'fsel';
  250.     const        OSType kAEOpenSelection        = 'sope';
  251.     const        OSType kFinderType                 = 'FNDR';
  252.     const        OSType kSysCreator                 = 'MACS';
  253.     
  254.     AppleEvent    aeEvent, aeReply;
  255.     AEDesc            myAddressDesc, aeDirDesc, listElem;
  256.     FSSpec            dirSpec, procSpec;
  257.     AEDesc            fileList;
  258.     OSErr                myErr;
  259.     ProcessSerialNumber        finderSN;
  260.     AliasHandle    DirAlias, FileAlias;
  261.                 
  262.     DirAlias= NULL;
  263.     FileAlias= NULL;
  264.     aeEvent.dataHandle= NULL;
  265.     
  266.         // find the finder on this machine 
  267. // failing here w/ no finder !?
  268.     myErr= FindProcessBySignature(kFinderType, &finderSN, &procSpec);
  269. //    if (myErr!=0) return procNotFound;
  270.     if (myErr != 0) { // do the hard way...
  271.         ProcessSerialNumber    processSN;
  272.     ProcessInfoRec infoRec;
  273.     Str31 processName;
  274.     FSSpec procSpec;
  275.     Boolean notFinder = true;
  276.  
  277.     processSN.lowLongOfPSN = kNoProcess;
  278.     processSN.highLongOfPSN = kNoProcess;
  279.         do {
  280.         myErr = GetNextProcess(&processSN);
  281.                 infoRec.processInfoLength = sizeof(ProcessInfoRec);
  282.                 infoRec.processName = (StringPtr)&processName;
  283.                 infoRec.processAppSpec = &procSpec;
  284.         GetProcessInformation(&processSN, &infoRec);
  285.                 if (infoRec.processSignature == kSysCreator 
  286.                  && infoRec.processType == kFinderType) {
  287.                     finderSN = processSN;
  288.                     notFinder = false;
  289.                     }
  290.             } while (notFinder && !myErr);    
  291.         }
  292.         
  293.         
  294.     myErr= AECreateDesc(typeProcessSerialNumber, (Ptr) &finderSN, sizeof(finderSN), 
  295.                                                 &myAddressDesc);
  296.     if (myErr != noErr) return myErr;
  297.     
  298.             // Create the FinderEvent 
  299.     myErr= AECreateAppleEvent(kFinderType, kAEOpenSelection, &myAddressDesc, 
  300.                                             kAutoGenerateReturnID, kAnyTransactionID, &aeEvent);
  301.     if (myErr != noErr) return myErr;
  302.     
  303.     // Now we build all the bits of an OpenSelection event. 
  304.     //  Basically, we need to create an alias for the item to open, and an alias to the parent 
  305.     // folder (directory) of that item.  We can also pass a list of files if we want.  
  306.     // Youll notice that for opening a finder window, the file and directory alias both point at the 
  307.     // folder itself. Make a spec for the parent folder  
  308.      
  309.         //  create the folder descriptor 
  310.     myErr= FSMakeFSSpec( fileToLaunch.vRefNum, fileToLaunch.parID, NULL, &dirSpec);
  311.     
  312.     myErr= NewAlias( NULL, &dirSpec, &DirAlias);
  313.     HLock( (Handle) DirAlias);
  314.     myErr= AECreateDesc(typeAlias, (Ptr) *DirAlias, GetHandleSize( (Handle) DirAlias), 
  315.                                 &aeDirDesc);
  316.     HUnlock( (Handle)DirAlias);
  317.     DisposHandle( (Handle) DirAlias);
  318.     
  319.     myErr= AEPutParamDesc( &aeEvent, keyDirectObject, &aeDirDesc);
  320.     if (myErr != noErr) return myErr;
  321.     myErr= AEDisposeDesc( &aeDirDesc);
  322.     
  323.             // create the file descriptor and add to aliasList 
  324.     myErr= NewAlias( NULL, &fileToLaunch, &FileAlias);
  325.     HLock( (Handle)FileAlias);
  326.     myErr= AECreateDesc(typeAlias, (Ptr)*FileAlias, GetHandleSize( (Handle)FileAlias), 
  327.                                     &listElem);
  328.     HUnlock( (Handle)FileAlias);
  329.     DisposHandle( (Handle)FileAlias);
  330.     
  331.         // Create the file  list 
  332.     myErr= AECreateList(NULL, 0, false, &fileList);
  333.     if (myErr != noErr) return myErr;
  334.     
  335.     myErr= AEPutDesc( &fileList, 0, &listElem);
  336.     (void) AEDisposeDesc( &listElem);
  337.     if (myErr != noErr) return myErr;
  338.     
  339.         // Add the file alias list to the event 
  340.     myErr= AEPutParamDesc( &aeEvent, aeSelectionKeyword, &fileList);
  341.     (void) AEDisposeDesc( &fileList);
  342.     if (myErr != noErr) return myErr;
  343.  
  344.     myErr= AESend( &aeEvent, &aeReply, kAENoReply + kAEAlwaysInteract + kAECanSwitchLayer,
  345.                                          kAENormalPriority, kAEDefaultTimeout, NULL, NULL);
  346.         
  347.     (void) AEDisposeDesc( &aeEvent);
  348.     return myErr;
  349. }
  350.  
  351. #endif
  352.  
  353.  
  354.  
  355. const char*  MacDirIDtoName( short vRefNum, long dirID, char* fname)
  356. {
  357. #ifdef WIN_MAC
  358.     static char    directory[PATH_MAX];
  359.   char     temp[PATH_MAX], * tmp;
  360.   CInfoPBRec   params;
  361.     short        err;
  362.     
  363.   temp[0] = '\0';
  364.   params.dirInfo.ioNamePtr = (StringPtr) directory;
  365.   params.dirInfo.ioDrParID = dirID;
  366.   do {
  367.     params.dirInfo.ioVRefNum = vRefNum;
  368.     params.dirInfo.ioFDirIndex = -1;
  369.     params.dirInfo.ioDrDirID = params.dirInfo.ioDrParID;
  370.     err = PBGetCatInfo (¶ms, FALSE);
  371. #ifdef COMP_THINKC
  372.         PtoCstr ((StringPtr) directory);
  373. #else
  374.         p2cstr ((StringPtr) directory);
  375. #endif
  376.     Nlm_StringCat(directory, DIRDELIMSTR);
  377.     Nlm_StringCat(directory, temp);
  378.     Nlm_StringCpy(temp, directory);
  379.   } while (params.dirInfo.ioDrDirID != fsRtDirID);
  380.  
  381.   tmp = Nlm_StringMove(directory, temp);
  382.   tmp = Nlm_StringMove(tmp, fname);
  383.   return directory;
  384. #else
  385.     return NULL;
  386. #endif
  387.  
  388.  
  389.  
  390. typedef void (*FileFunc)(const char*);
  391.  
  392. short HandleFileEvent( long inEvent, long outReply, long inNumber,
  393.                                             long fileFunc)
  394. {                    
  395. #ifdef WIN_MAC
  396.     AppleEvent    *aeEvent, *aeReply;
  397.     AEDescList    docList;            
  398.     short                err;
  399.     long                nitems, item;
  400.     FileFunc    * callFileFunc;
  401.     
  402.     if (!inEvent || !fileFunc) return;
  403.     aeEvent = (AppleEvent*) inEvent;
  404.     aeReply = (AppleEvent*) outReply;
  405.     callFileFunc = (FileFunc*) fileFunc;
  406.     
  407.     err= AEGetParamDesc( aeEvent, keyDirectObject, typeAEList, &docList);
  408.     if (err) return;
  409.     err= AECountItems( &docList, &nitems);
  410.     if (err) return;
  411.     for (item= 1; item<=nitems; item++) {
  412.         AEKeyword        keywd;
  413.         DescType        returnedType;
  414.         FSSpec            aFileSpec;
  415.         Size                actualSize;
  416.         const char * pathname;
  417.         
  418.         err= AEGetNthPtr( &docList, item, typeFSS, &keywd, &returnedType,
  419.                                             (void*)&aFileSpec, sizeof(aFileSpec), &actualSize);
  420.         if (err) break; // continue ?
  421.         
  422. #ifdef COMP_THINKC
  423.         PtoCstr ((StringPtr) aFileSpec.name);
  424. #else
  425.         p2cstr ((StringPtr) aFileSpec.name);
  426. #endif
  427.         pathname= MacDirIDtoName( aFileSpec.vRefNum, aFileSpec.parID,
  428.                                                          (char*) aFileSpec.name);
  429.         (*callFileFunc)( pathname); // this->OpenDocument( filename);
  430.         }
  431.     (void) AEDisposeDesc( &docList);
  432.     return err;
  433. #else
  434.     return -1;
  435. #endif
  436. }
  437.  
  438.  
  439.  
  440. #ifdef WIN_MAC
  441. /* quick hack to check child apps by psn */
  442. ProcessSerialNumber  gChildProcessSN;
  443. #else
  444. long         gChildProcessSN;
  445. #endif
  446.  
  447.  
  448. char* Fullpathname( char* filename, Nlm_Boolean forceprogpath)
  449. {
  450.     static char    pathname [512]; /* ?? is there a 256 char limit on mac paths ?? */
  451.   Nlm_CharPtr          ptr;
  452.   Nlm_Boolean addpath;
  453.   
  454.   addpath= forceprogpath 
  455.     || (Nlm_StringChr(filename, DIRDELIMCHR) == NULL);
  456. #ifdef WIN_MSWIN
  457.   if (filename[1] == ':' || filename[0] == DIRDELIMCHR)
  458.     addpath= false;
  459. #endif
  460. #ifdef OS_UNIX
  461.   if (filename[0] == DIRDELIMCHR) addpath= false;
  462. #endif
  463.  
  464.     if (!addpath) 
  465.         Nlm_StringNCpy ((Nlm_CharPtr) pathname, filename, sizeof (pathname));
  466.     else {
  467.         Nlm_ProgramPath((Nlm_CharPtr) pathname, sizeof (pathname));
  468.         ptr = Nlm_StringRChr((Nlm_CharPtr) pathname, DIRDELIMCHR);
  469.         if (ptr)  *ptr = '\0';
  470.         Nlm_FileBuildPath((Nlm_CharPtr) pathname, NULL, filename);
  471.         }
  472.     return pathname;
  473. }
  474.  
  475.  
  476.  
  477. extern Nlm_Boolean Dgg_LaunchApp( char* appName, char* commandline,
  478.                                                     char* Stdin, char* Stdout, char* Stderr)
  479. {
  480. #ifdef WIN_MAC
  481.   OSErr                              err;
  482.   FSSpec               fsspec;
  483.   long                 gval;
  484.   LaunchParamBlockRec  lparm;
  485.     //unsigned char *      pathname[256];  
  486.   Nlm_CharPtr          pathname, ptr;
  487.   Nlm_Boolean          rsult;
  488.  
  489.   rsult = FALSE;
  490.   if (Gestalt (gestaltSystemVersion, &gval) == noErr && (short) gval >= 7 * 256) {
  491.     if (appName && *appName) {
  492.             pathname= Fullpathname( appName, false);
  493.             Nlm_CtoPstr((Nlm_CharPtr) pathname);
  494.             err = FSMakeFSSpec(0, 0, (unsigned char*)pathname, &(fsspec));
  495.             
  496.             if (Stdin || Stdout || Stderr) {
  497.                 if (!commandline) commandline= MemNew(1);
  498.                 if (Stdin) {
  499.                     pathname= Fullpathname(Stdin,false);
  500.                     commandline= MemMore( commandline, StrLen(commandline) + 6 + StrLen(pathname));
  501.                     StrCat( commandline, " < '");
  502.                     StrCat( commandline, pathname);
  503.                     StrCat( commandline, "'");
  504.                     }
  505.                 if (Stdout) {
  506.                     pathname= Fullpathname(Stdout,false);
  507.                     commandline= MemMore( commandline, StrLen(commandline) + 6 + StrLen(pathname));
  508.                     StrCat( commandline, " > '");
  509.                     StrCat( commandline, pathname);
  510.                     StrCat( commandline, "'");
  511.                     }
  512.                 if (Stderr) {
  513.                     pathname= Fullpathname(Stderr,false);
  514.                     commandline= MemMore( commandline, StrLen(commandline) + 6 + StrLen(pathname));
  515.                     StrCat( commandline, " 2> '");
  516.                     StrCat( commandline, pathname);
  517.                     StrCat( commandline, "'");
  518.                     }
  519.                 }
  520.                 
  521.             if (commandline) {
  522.                 /* only good for launching an application, not a document... */
  523.                 EventRecord hlevt;
  524.               AppParametersPtr appParams;
  525.                 long    len;
  526.  
  527.                 MemSet( &lparm, 0, sizeof(lparm));
  528.               len= 2 + StrLen(appName) + StrLen( commandline);
  529.                 hlevt.what= kHighLevelEvent;
  530.                 hlevt.message= 'DClp'; // ID of sender == DClap application
  531.                 hlevt.where.v= 'Cm';  
  532.                 hlevt.where.h= 'dl'; //(Point)'Cmdl';     // command line event type
  533.                 
  534.               appParams= (AppParametersPtr) MemNew( sizeof(AppParameters) + len + 2);
  535.                 appParams->theMsgEvent= hlevt;
  536.                 appParams->eventRefCon= 'Cmdl'; // ?? again
  537.                 ptr= (char*) &(appParams->messageLength) + sizeof(long);
  538.                 StrCpy( ptr, appName);
  539.                 StrCat( ptr, " ");
  540.                 StrCat( ptr, commandline); 
  541.                 appParams->messageLength= len;
  542.                 lparm.launchAppParameters = appParams;
  543.                     
  544.                 lparm.launchBlockID = extendedBlock;
  545.                 lparm.launchEPBLength = extendedBlockLen;
  546.                 lparm.launchFileFlags = 0;
  547.                 lparm.launchControlFlags = launchContinue + launchNoFileFlags;
  548.                 lparm.launchAppSpec = &fsspec;
  549.                 err= LaunchApplication (&lparm);
  550.                 rsult= (err == noErr);
  551.                 gChildProcessSN= lparm.launchProcessSN;
  552.                 
  553.                 MemFree( appParams);
  554.                 }
  555.             else {
  556.                  rsult = (Nlm_Boolean) (LaunchFileSpecThruFinder( fsspec)  == noErr);
  557.                  }
  558.         }
  559.       }
  560.   return rsult;
  561. #endif
  562.  
  563. #ifdef WIN_MSWIN
  564.   Nlm_Char     ch;
  565.   Nlm_Int2     i, j, k;
  566.   Nlm_Uint2         val;
  567.   char         lpszCmdLine[256];
  568.   char      *  pathname, *ptail;
  569.   Nlm_Boolean  rsult;
  570.  
  571.   rsult = FALSE;
  572.   if ((appName && *appName) || (commandline && *commandline)) {
  573.   
  574.         if (Stdin || Stdout || Stderr) {
  575.             if (!commandline) commandline= MemNew(1);
  576.             if (Stdin) {
  577.                 pathname= Fullpathname(Stdin,false);
  578.                 commandline= MemMore( commandline, StrLen(commandline) + 6 + StrLen(pathname));
  579.                 StrCat( commandline, " < ");
  580.                 StrCat( commandline, pathname);
  581.                 }
  582.             if (Stdout) {
  583.                 pathname= Fullpathname(Stdout,false);
  584.                 commandline= MemMore( commandline, StrLen(commandline) + 6 + StrLen(pathname));
  585.                 StrCat( commandline, " > ");
  586.                 StrCat( commandline, pathname);
  587.                 }
  588.             if (Stderr) {
  589.                 pathname= Fullpathname(Stderr,false);
  590.                 commandline= MemMore( commandline, StrLen(commandline) + 6 + StrLen(pathname));
  591.                 StrCat( commandline, " 2> ");
  592.                 StrCat( commandline, pathname);
  593.                 }
  594.             }
  595.  
  596. #ifdef WIN16
  597.         *lpszCmdLine= 0;
  598.         if (appName) { 
  599.         Nlm_StringNCpy( lpszCmdLine, appName, sizeof(lpszCmdLine));
  600.         Nlm_StringNCat( lpszCmdLine, " ", sizeof(lpszCmdLine));
  601.         }
  602.     Nlm_StringNCat( lpszCmdLine, commandline, sizeof(lpszCmdLine));
  603.     
  604.     val= WinExec (lpszCmdLine, SW_SHOW);
  605.     gChildProcessSN= val;
  606.     rsult= (val >= 32);
  607. #else
  608.     {
  609.     STARTUPINFO  si;
  610.     PROCESS_INFORMATION pri;
  611.     
  612.     memset(&pri, 0, sizeof(pri));
  613.     memset(&si, 0, sizeof(si));
  614.     si.cb= sizeof(si); /* !?? */
  615.     si.wShowWindow= SW_SHOW;
  616. #if 0
  617.         if (Stdin)  { freopen( Stdin, "r", stdin); si.hStdInput= stdin; }
  618.         if (Stdout) { freopen( Stdout, "a", stdout); si.hStdOutput= stdout; }
  619.         if (Stderr) { freopen( Stderr, "a", stderr); si.hStdError= stderr; }
  620.     si.dwFlags= STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES; 
  621. #else
  622.     si.dwFlags= 0; 
  623. #endif
  624.  
  625. #if 0
  626.       pathname= Fullpathname(appName,false);
  627.       ptail = pathname + StrLen(pathname) - 4;
  628.       if (StrICmp( ptail, ".exe") != 0)  StrCat( pathname, ".exe");
  629.     Nlm_StringNCpy( lpszCmdLine, commandline, sizeof(lpszCmdLine));
  630.     /* ! cmdline redirects are being intercepted by win-os ! */
  631.     rsult= CreateProcess( pathname, lpszCmdLine, NULL, NULL, true, 
  632. #else
  633.         *lpszCmdLine= 0;
  634.         if (appName) { 
  635.         Nlm_StringNCpy( lpszCmdLine, appName, sizeof(lpszCmdLine));
  636.         Nlm_StringNCat( lpszCmdLine, " ", sizeof(lpszCmdLine));
  637.         }
  638.     Nlm_StringNCat( lpszCmdLine, commandline, sizeof(lpszCmdLine));
  639.  
  640.     rsult= CreateProcess( NULL, lpszCmdLine, NULL, NULL, true, 
  641. #endif
  642.             DETACHED_PROCESS,/*CREATE_NEW_CONSOLE, */ 
  643.             NULL/*Env*/, 
  644.             NULL/*CurDir*/, 
  645.             &si, &pri);
  646.     if (rsult) {
  647.       gChildProcessSN= (long) pri.dwProcessId; /* hProcess ; */
  648.       }
  649.     }
  650. #endif
  651.   }
  652.   return rsult;
  653. #endif
  654.  
  655.  
  656. #ifdef WIN_MOTIF
  657.   Nlm_Boolean  rsult;
  658.   long              status;
  659.  
  660.   rsult = FALSE;
  661.   if ((appName && *appName) || (commandline && *commandline)) {
  662.     int          narg = 0, done;
  663.     char        *cmdline, **argv, *cp, *ep;
  664.     ulong     len;
  665.     
  666.         len= StrLen(appName) + StrLen(commandline) + 3;
  667.         cmdline= MemNew(len);
  668.         *cmdline= 0;
  669.         if (appName) { 
  670.             Nlm_StringCpy( cmdline, appName);
  671.             Nlm_StringCat( cmdline, " ");
  672.             }
  673.         Nlm_StringCat( cmdline, commandline);
  674.         
  675.        /* need to pop the cmdline args into a vector !*/
  676.     cp= cmdline; done= FALSE;
  677.     argv= (char**) Nlm_MemNew(sizeof(Nlm_CharPtr));
  678.     while (!done) {
  679.        while (*cp && *cp <= ' ') cp++;
  680.        if (*cp == '"') { for (ep= cp+1; *ep && *ep != '"'; ep++) ; }
  681.        else if (*cp == '\'') { for (ep= cp+1; *ep && *ep != '\''; ep++) ; }
  682.        else if (*cp) { for (ep= cp+1; *ep && *ep>' '; ep++) ; }
  683.        if (*ep) *ep= '\0';  else done= TRUE;
  684.        narg++;
  685.        argv= (char**) Nlm_MemMore(argv, (narg+1) * sizeof(Nlm_CharPtr));
  686. /*xdebugp("Dgg_LaunchFile argv[i]= %s\n",cp);*/
  687.        argv[narg-1]= cp;
  688.        cp= ep+1;
  689.        }
  690.     argv[narg]= NULL;
  691.  
  692. #ifdef OS_UNIX
  693.     status =  fork();
  694.     if (status == 0) {
  695.         /* if status==0, we are now the child process */
  696.             if (Stdin) { freopen( Stdin, "r", stdin); }
  697.             if (Stdout) { freopen( Stdout, "a", stdout); }
  698.             if (Stderr) { freopen( Stderr, "a", stderr); }
  699. #if 0
  700. /* DEBUG */
  701.             fprintf( stdout, "\n This is new stdout=%s\n",Stdout);
  702.             fprintf( stderr, "\n This is new stderr=%s\n",Stderr);
  703.             fprintf( stdout, "\n Dgg_LaunchApp(%s,%s,\n    %s,%s,%s);\n",
  704.                     appName,commandline, Stdin, Stdout, Stderr);
  705.             fprintf( stdout, " arg[0]=%s\n",argv[0]);
  706.             fflush( stdout); 
  707.             fflush( stderr);
  708.             /*_exit(-1); */        
  709. #endif
  710.       execvp( argv[0], argv);
  711.       /* if (stillhere) Nlm_Message (MSG_FATAL, "Application launch failed"); */
  712.       _exit(-1); /* app launch failed if we get here... */
  713.         } 
  714.     else if (status != -1) {
  715.         /* here status == child process id, need to save for child app handler */
  716.         gChildProcessSN= status;
  717.       rsult = TRUE;
  718.         }
  719.     else
  720.            Nlm_Message (MSG_OK, "Application launch failed");  
  721.  
  722. #endif
  723. #ifdef OS_VMS
  724.         /* ?? for VMS -- how do we change child process stdin/stdout/stderr ?? */
  725.     status = execvp( argv[0], argv);
  726.     if ( status == -1 ) {
  727.       /*Nlm_Message (MSG_FATAL, "Application launch failed"); */
  728.         } 
  729.     else {
  730.         gChildProcessSN= status;
  731.         /* here status == child process id, need to save for child app handler */
  732.       rsult = TRUE;
  733.         }
  734. #endif
  735.     Nlm_MemFree(cmdline);
  736.   }
  737.   return rsult;
  738. #endif
  739. }
  740.